package com.vpx.server.service.system.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vpx.server.bean.entity.SysExceptionLog;
import com.vpx.server.bean.info.base.UserInfo;
import com.vpx.server.bean.info.system.SysExceptionLogInfo;
import com.vpx.server.bean.order.base.BaseDeleteOrder;
import com.vpx.server.bean.order.base.BaseIdOrder;
import com.vpx.server.bean.order.system.SysExceptionLogOrder;
import com.vpx.server.bean.order.system.SysExceptionLogPageOrder;
import com.vpx.server.bean.result.base.BaseDeleteResult;
import com.vpx.server.bean.result.system.SysExceptionLogPageResult;
import com.vpx.server.bean.result.system.SysExceptionLogResult;
import com.vpx.server.framework.aop.ServiceHandler;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.framework.exception.BizException;
import com.vpx.server.framework.utils.DateUtils;
import com.vpx.server.mapper.system.SysExceptionLogMapper;
import com.vpx.server.service.system.SysExceptionLogService;
import com.vpx.server.utils.UserLoginUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module: 异常日志
 * @description: 异常日志服务实现
 * @date 2021/09/13 11:40
 */
@Service
public class SysExceptionLogServiceImpl implements SysExceptionLogService {

    @Autowired
    private SysExceptionLogMapper sysExceptionLogMapper;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public ResultMsg insert(SysExceptionLogOrder order) {
        Date now = DateUtils.now();
        SysExceptionLog sysExceptionLog = new SysExceptionLog();
        sysExceptionLog.from(order);
        UserInfo userInfo =UserLoginUtils.getCurrentUser();
        if(userInfo!=null){
            sysExceptionLog.setCreateUserId(userInfo.getId());
        }
        sysExceptionLog.setCreateTime(now);
        sysExceptionLog.setUpdateTime(now);
        sysExceptionLogMapper.insert(sysExceptionLog);
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "删除异常日志", isTransaction = true)
    public ResultMsg deleteById(BaseDeleteOrder order) {
        BaseDeleteResult result = new BaseDeleteResult();
        result.setDeleteConfirm(false);
        sysExceptionLogMapper.deleteById(order.getId());
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "查询异常日志")
    public ResultMsg<SysExceptionLogResult> findById(BaseIdOrder order) {
        SysExceptionLog exceptionLog = sysExceptionLogMapper.selectById(order.getId());
        if (exceptionLog == null) {
            throw new BizException("未查询到异常日志信息");
        }
        SysExceptionLogResult result = new SysExceptionLogResult();
        result.from(exceptionLog);
        return ResultMsg.success(result);
    }

    @Override
    @ServiceHandler(memo = "分页查询异常日志")
    public ResultMsg<SysExceptionLogPageResult> pageQuery(SysExceptionLogPageOrder order) {
        if (order.getPageNum() != null && order.getPageSize() != null) {
            PageHelper.startPage(order.getPageNum(), order.getPageSize());
        }
        if(StringUtils.isNotBlank(order.getExceptionDateStart())) {
            String beginTime = order.getExceptionDateStart();
            order.setExceptionDateStart(DateUtil.parse(beginTime, DatePattern.NORM_DATETIME_PATTERN).getTime()+"");
        }
        if(StringUtils.isNotBlank(order.getExceptionDateEnd())) {
            String endTime = order.getExceptionDateEnd();
            order.setExceptionDateEnd(DateUtil.parse(endTime, DatePattern.NORM_DATETIME_PATTERN).getTime()+"");
        }
        List<SysExceptionLogInfo> exceptionLogs = sysExceptionLogMapper.pageQuery(order);
        PageInfo<SysExceptionLogInfo> pageInfo = new PageInfo<SysExceptionLogInfo>(exceptionLogs);
        SysExceptionLogPageResult result = new SysExceptionLogPageResult();
        result.setPageInfo(pageInfo);
        return ResultMsg.success(result);
    }
}
